﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KeystrokeConverter
{
    class Program
    {
        public static bool headsetMode;
        public static bool running;
        public static CommandManager manager;

        public static void Main(string[] args)
        {
            headsetMode = false;

            running = true;
            manager = new CommandManager();
            if (!manager.OpenConnection())
            {
                Console.WriteLine("Couldn't open connection");
                running = false;
            }
            else
            {
                Console.WriteLine("Started Reading! Press Escape to stop...");
            }
            while (running)
            {
                if (headsetMode) readHeadsetLine();
                else readKeystroke();
            }
                manager.SendCommand(AvailableCommands.cmdStop);
                manager.CloseConnection();
                Console.WriteLine("Processing stopped! press any key to exit...");
                Console.ReadKey();
        }

        static void readKeystroke()
        {
            ConsoleKeyInfo info = Console.ReadKey();
            Console.WriteLine();
            Console.WriteLine("Read Key {0}", info.Key);
            switch (info.Key)
            {
                case ConsoleKey.Escape:
                    running = false;
                    break;
                case ConsoleKey.W:
                    Console.WriteLine("Forward Motion Detected");
                    manager.SendCommand(AvailableCommands.cmdFwd);
                    break;
                case ConsoleKey.A:
                    Console.WriteLine("Left Turn Detected");
                    manager.SendCommand(AvailableCommands.cmdLeft);
                    break;
                case ConsoleKey.S:
                    Console.WriteLine("Reverse Motion Detected");
                    manager.SendCommand(AvailableCommands.cmdback); break;
                case ConsoleKey.D:
                    Console.WriteLine("Right Turn Detected");
                    manager.SendCommand(AvailableCommands.cmdRight);
                    break;
                case ConsoleKey.R:
                    Console.WriteLine("Raise Detected");
                    manager.SendCommand(AvailableCommands.cmdRaise);
                    break;
                case ConsoleKey.F:
                    Console.WriteLine("Lower Detected");
                    manager.SendCommand(AvailableCommands.cmdLower);
                    break;
                case ConsoleKey.Spacebar:
                    Console.WriteLine("Stop command detected");
                    manager.SendCommand(AvailableCommands.cmdStop);
                    break;
            }
        }

        static void readHeadsetLine()
        {
            string s = Console.ReadLine();
            switch (s)
            {
                case "":
                    break;
            }
        }
    }
}
